﻿Public Class HDCurveForm

    Private _pi As ProjectItem
    Private _rti As RefTypeItem
    Private _sti As StartUpTypeBase
    Private _sdti As StartUpDetailTypeItem
    Private _activeType As Integer '0:所有终端; 1:终端; 2:启停纪录; 3:详细启停纪录
    Private Const DISABLED_VALUE As Integer = -300
    Private Const DISABLED_TEXT As String = "--"

#Region " 构造函数 "

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _activeType = 0
        _pi = Nothing
    End Sub

    Public Sub New(ByVal pi As ProjectItem)

        Me.New()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _activeType = 1
        _pi = pi

        Me.SetText(pi)

        Me.Size = New Size(Me.Width - Me.scTerminal.SplitterDistance, Me.Height)
        Me.scTerminal.Panel1.Enabled = False
        Me.scTerminal.Panel1Collapsed = True

    End Sub

    Public Sub New(ByVal pi As ProjectItem, ByVal sti As StartUpTypeBase)

        Me.New()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _activeType = 2
        _pi = pi
        _sti = sti

        Me.SetText(pi)

        Me.Text &= String.Format(" ({0} ～ {1})", _
                                IIf(sti.BeginTime = Nothing, String.Empty, sti.BeginTime.ToString), _
                                IIf(sti.EndTime = Nothing, String.Empty, sti.EndTime.ToString))

        Me.Size = New Size(Me.Width - Me.scTerminal.SplitterDistance, Me.Height)
        Me.scTerminal.Panel1.Enabled = False
        Me.scTerminal.Panel1Collapsed = True
        Me.scMain.Panel1.Enabled = False
        Me.scMain.Panel1Collapsed = True

    End Sub

    Public Sub New(ByVal pi As ProjectItem, ByVal sdti As StartUpDetailTypeItem)

        Me.New()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _activeType = 3
        _pi = pi
        _sdti = sdti

        Me.SetText(pi)

        Me.Text &= String.Format(" ({0} ～ {1})", _
                                IIf(sdti.BeginTime = Nothing, String.Empty, sdti.BeginTime.ToString), _
                                IIf(sdti.EndTime = Nothing, String.Empty, sdti.EndTime.ToString))

        Me.Size = New Size(Me.Width - Me.scTerminal.SplitterDistance, Me.Height)
        Me.scTerminal.Panel1.Enabled = False
        Me.scTerminal.Panel1Collapsed = True
        Me.scMain.Panel1.Enabled = False
        Me.scMain.Panel1Collapsed = True

    End Sub

    Private Sub SetText(ByVal pi As ProjectItem)
        If pi IsNot Nothing Then
            Me.Text &= String.Format(" [终端:{0}]", pi.ProjectName)
        Else
            Me.Text &= " [终端:未知]"
        End If
    End Sub

#End Region

    Private Sub HDViewForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.CenterToParent()

        Me.dtpBeginTime.Value = New DateTime(Now.Year, Now.Month, Now.Day)
        Me.dtpEndTime.Value = Me.dtpBeginTime.Value.AddDays(1)

        Select Case _activeType
            Case 0
                LoadProjectList(String.Empty)

            Case 2
                If _sti IsNot Nothing Then
                    'SearchHistoryData(_sti.ID, _sti.BeginTime, _sti.EndTime)
                End If

            Case 3
                If _sdti IsNot Nothing Then
                    'SearchHistoryData(_sdti.ParentID, _sdti.BeginTime, _sdti.EndTime)
                End If

        End Select

    End Sub

    Private Sub HDViewForm_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If Asc(e.KeyChar) = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Search_Startup()

    End Sub

    Private Sub dgvStartup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvStartup.Click

        Search_StartDetail()

    End Sub

    Private Sub dgvStartupDetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvStartupDetail.Click

        Search_HistoryData()

    End Sub

    Private Sub tvPrjList_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvPrjList.AfterSelect
        If Me.tvPrjList.SelectedNode IsNot Nothing Then
            _pi = Me.tvPrjList.SelectedNode.Tag

            If _pi Is Nothing Then Exit Sub

            Dim netid As Integer = 1
            Dim rts As RefTypeService = New RefTypeService(_pi)
            Dim rtgl As IList(Of RefTypeItem)
            Dim rti As RefTypeItem = Nothing

            rtgl = rts.GeRefGroupList(netid)
            If rtgl IsNot Nothing AndAlso rtgl.Count > 0 Then
                rti = rtgl(0)

                Dim atps As AiPortTypeService = New AiPortTypeService(rti)
                Dim atpgl As IList(Of AiPortTypeItem)
                Dim atpi As AiPortTypeItem = Nothing

                atpgl = atps.GetAiGroupList()

                For Each atpi In atpgl
                    rti.AiPortTypeGroupList.Add(atpi)
                Next
            End If

            _rti = rti
        End If
    End Sub

    Private Sub txtPrjName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPrjName.TextChanged

        LoadProjectList(Me.txtPrjName.Text)

    End Sub

    Private Sub dgvHisData_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)

        If TypeOf e.Value Is Single Then
            If e.Value = DISABLED_VALUE Then
                e.CellStyle.Format = Nothing
                e.Value = DISABLED_TEXT
            End If
        End If

    End Sub

    '查询启停记录
    Private Sub Search_Startup()

        Me.bsStartup.DataSource = Nothing
        Me.bsStartupDetail.DataSource = Nothing
        'Me.bsHisData.DataSource = Nothing

        If _pi Is Nothing Then Exit Sub

        Dim beginTime As DateTime = Me.dtpBeginTime.Value
        Dim endTime As DateTime = Me.dtpEndTime.Value
        Dim sts As New StartUpTypeService(_pi)
        Dim il As IList(Of StartUpTypeItem)

        il = sts.GetStartUpGroupList(beginTime, endTime, False)

        Me.bsStartup.DataSource = il

    End Sub

    '查询详细启停记录
    Private Sub Search_StartDetail()

        Me.bsStartupDetail.DataSource = Nothing
        'Me.bsHisData.DataSource = Nothing

        If _pi Is Nothing Then Exit Sub

        Dim sti As StartUpTypeItem = Me.bsStartup.Current
        If sti IsNot Nothing Then
            Dim sdti As New StartUpDetailTypeService(_pi, sti)
            Dim il As IList(Of StartUpDetailTypeItem)

            il = sdti.GetStartUpDetailGroupList(sti.ID)

            Me.bsStartupDetail.DataSource = il


            ''If il Is Nothing OrElse il.Count <= 0 Then '当无详细启停时，显示启停的历史数据
            ''Me.bsHisData.DataSource = Nothing

            Me.grpHistoryCurve.Text = String.Format("{0} - {1} ～ {2}", _
                                                    Me.grpHistoryCurve.Tag, _
                                                    IIf(sti.BeginTime = Nothing, String.Empty, sti.BeginTime), _
                                                    IIf(sti.EndTime = Nothing, String.Empty, sti.EndTime))

            InitCurveControl()

            Dim ild As IList(Of HDataTypeItem)
            ild = SearchHistoryData(sti.ID, sti.BeginTime, sti.EndTime)

            Me.hccHisCurve.RunDrawCurve(ild, sti.BeginTime, sti.EndTime)

            ''End If
        End If

    End Sub

    '查询历史数据
    Private Sub Search_HistoryData()

        'Me.bsHisData.DataSource = Nothing

        If _pi Is Nothing Then Exit Sub

        Dim sdti As StartUpDetailTypeItem
        sdti = Me.bsStartupDetail.Current

        If sdti IsNot Nothing Then

            Me.grpHistoryCurve.Text = String.Format("{0} - {1} ～ {2}", _
                                                    Me.grpHistoryCurve.Tag, _
                                                    IIf(sdti.BeginTime = Nothing, String.Empty, sdti.BeginTime), _
                                                    IIf(sdti.EndTime = Nothing, String.Empty, sdti.EndTime))

            InitCurveControl()

            Dim ild As IList(Of HDataTypeItem)
            ild = SearchHistoryData(sdti.ParentID, sdti.BeginTime, sdti.EndTime)

            Me.hccHisCurve.RunDrawCurve(ild, sdti.BeginTime, sdti.EndTime)

        End If

    End Sub

    '根据时间段查询历史数据
    Private Function SearchHistoryData(ByVal parentID As Integer, ByVal beginTime As DateTime, ByVal endTime As DateTime) As IList(Of HDataTypeItem)

        If _pi Is Nothing Then Return Nothing

        Dim hdts As New HDataTypeService(_pi)

        Dim ild As IList(Of HDataTypeItem)

        ild = hdts.GetHDataGroupList(parentID, beginTime, endTime)

        Return ild

    End Function

    Private Function SearchHistoryData(ByVal parentID As Integer) As IList(Of HDataTypeItem)

        Return SearchHistoryData(parentID, Nothing, Nothing)

    End Function


    '加载终端列表
    Private Sub LoadProjectList(ByVal pattern As String)

        Me.tvPrjList.Nodes.Clear()

        If MainForm.ProjectList IsNot Nothing Then
            For Each row As DataGridViewRow In MainForm.ProjectList.Values
                Dim pi As ProjectItem
                pi = row.Tag
                If pi IsNot Nothing Then
                    Try
                        If pi.ProjectName.IndexOf(pattern) >= 0 Then
                            Dim node As New TreeNode()
                            node.Name = pi.ProjectMacID
                            node.Text = pi.ProjectName
                            node.Tag = pi
                            Me.tvPrjList.Nodes.Add(node)
                        End If
                    Catch ex As Exception
                        Debug.WriteLine(ex.Message)
                    End Try
                End If
            Next
        End If

    End Sub

#Region " 曲线操作 "

    Private Sub InitCurveControl()

        If _pi Is Nothing Then Exit Sub
        If _rti Is Nothing Then Exit Sub

        Me.hccHisCurve.RefTypeInfo = _rti
        Me.hccHisCurve.InitCurveControl()

    End Sub

#End Region

End Class